<template>
  <div class="details">
    <div class="header">
      <div style="text-align:left">
        <el-button type="text"
                   size="small"
                   @click="$router.back()">返回</el-button>
      </div>
      <div class="title">{{ article.title }}</div>
      <div class="title_sub">
        <span>
          <strong>所属栏目：</strong>
          {{ article.category && article.category.name }}
        </span>
        <span>
          <strong>发布时间：</strong>
          {{ article.createdAt | fmtDate }}
        </span>
        <span>
          <strong>浏览量:</strong>
          {{ article.readTimes }}次
        </span>
        <span>
          <strong>点赞次数:</strong>
          {{ article.thumpUp }}次
        </span>
      </div>
    </div>
    <div class="content"
         v-html="article.content" />
    <!-- 评论 -->
    <span class="item"
          style="float: right; margin: 10px 20px 0 0">
      <i class="iconfont icon-dianzan"
         @click="thumpUp"
         style="cursor:pointer"></i>
    </span>
    <div class="comment">
      <div>
        <div style="text-align:left; font-size:12px;color:#666;line-height:3em;height:3em">我要评论</div>
        <el-input v-model="form.comment"
                  type="textarea" />
        <div style="text-align:right;margin:1em 0">
          <el-button type="primary"
                     size="small"
                     @click="submitCommentHandler">提交</el-button>
        </div>
      </div>
      <ul style="font-size:12px;color:#666">
        <li v-for="comment in article.comments"
            :key="comment.id"
            style="border-bottom:1px solid #ededed;padding-bottom:1em;margin:.5em 0">
          <div v-if="comment.baseUser"
               style="width:50px;float:left;overflow:hidden">
            <img style="width:50px;height:50px;border-radius:50%;"
                 :src="comment.baseUser.userFace"
                 alt>
          </div>
          <div style="margin-left:60px;padding:0 .5em">
            <div style="line-height:2em">
              <strong>{{ comment.baseUser.realname }}</strong>
            </div>
            <div style="line-height:2em">{{ comment.comment }}</div>
            <div style="line-height:2em">评论时间： {{ comment.commentTime | fmtDate }}</div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { getArticle, thumpUpArticle } from '@/api/article'
import { createComment } from '@/api/comment'
export default {
  data() {
    return {
      article: {},
      form: {},
      flag: 0,
      id: NaN
    }
  },
  computed: {},
  created() {
    this.id = this.$route.query.id
    this.findArticleById()
  },
  methods: {
    async findArticleById() {
      const res = await getArticle({ id: this.id })
      this.article = res.data
    },
    async submitCommentHandler() {
      if (this.form.comment && this.form.comment.length > 5) {
        const res = await createComment({ ...this.form, article_id: this.article.id })
        console.log(res)
        this.$notify.success({
          title: '成功',
          message: res.message
        })
        this.form.comment = ''
      } else {
        this.$message({ type: 'error', message: '评论内容不能少于5个字符！' })
      }
    },
    async thumpUp() {
      await thumpUpArticle(this.id).then(() => {
        this.flag = 1;
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.details {
  columns: #666666;
  .header {
    text-align: center;

    padding: 1em 2em;
    border-bottom: 1px solid #ededed;
    .title {
      font-size: 20px;
      line-height: 2em;
    }
    .title_sub {
      color: #999;
      font-size: 12px;
      line-height: 2em;
    }
  }
  .content {
    padding: 1em 0;
  }
  .comment {
    border-top: 1px solid #ededed;
    padding: 1em 0;
  }
}
</style>
